package com.example.ercmssystem2.repository;

import com.example.ercmssystem2.entity.EventResource;
import com.example.ercmssystem2.entity.Event;
import com.example.ercmssystem2.entity.Resource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EventResourceRepository extends JpaRepository<EventResource, Long> {
    
    @Query("SELECT er.resource.id FROM EventResource er WHERE er.event.id = :eventId")
    List<Long> findResourceIdsByEventId(@Param("eventId") Long eventId);
    
    @Query("SELECT er.event.id FROM EventResource er WHERE er.resource.id = :resourceId")
    List<Long> findEventIdsByResourceId(@Param("resourceId") Long resourceId);
    
    void deleteByEventIdAndResourceId(Long eventId, Long resourceId);
    
    EventResource findByEventAndResource(Event event, Resource resource);
    
    @Query("SELECT er FROM EventResource er WHERE er.event.id = :eventId")
    List<EventResource> findByEventId(@Param("eventId") Long eventId);
    
    List<EventResource> findByEventIdAndStatusIn(Long eventId, List<String> statuses);
    
    @Modifying
    @Query(value = "DELETE FROM event_resource WHERE event_id = :eventId", nativeQuery = true)
    void deleteAllByEventId(@Param("eventId") Long eventId);
} 